define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'scanwork/active_trace_code/index' + location.search,
                    del_url: 'scanwork/active_trace_code/del',
                    multi_url: 'scanwork/active_trace_code/multi',
                    table: 'trace_codes',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'trace_code', title: __('追溯码'), operate: 'LIKE'},
                        {field: 'order_no', title: __('订单号'), operate: 'LIKE'},
                        {field: 'product_name', title: __('产品名称'), operate: 'LIKE'},
                        {field: 'model_name', title: __('型号'), operate: 'LIKE'},
                        {field: 'process_name', title: __('工序'), operate: 'LIKE'},
                        {field: 'worker_name', title: __('员工'), operate: 'LIKE'},
                        {field: 'quantity', title: __('数量'), operate: false},
                        {field: 'work_hours', title: __('工作时长'), operate: false},
                        {field: 'total_wage', title: __('总工资'), operate: false},
                        {field: 'audit_time', title: __('审核时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', autocomplete: false},
                        {field: 'status', title: __('状态'), searchList: {"1":__('正常'),"0":__('禁用')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 绑定下载二维码事件
            $(document).on("click", ".btn-multi[data-params*='action=downloadQrCode']", function () {
                var ids = Table.api.selectedids(table);
                if (ids.length === 0) {
                    Toastr.error("请选择要下载的追溯码");
                    return;
                }
                var url = Fast.api.fixurl('scanwork/active_trace_code/downloadQrCode?ids=' + ids.join(','));
                window.open(url);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
